﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using CardsAlbumDAL;

namespace CardAlbumWcfService
{
    public class CardAlbumService : ICardAlbumService
    {
        public CardDTO GetCardById(string cardId)
        {
            CardsAlbumModel albumModel = new CardsAlbumModel();

            int parsedId = int.Parse(cardId);
            Card card = albumModel.Cards.FirstOrDefault(x => x.Id == parsedId);

            var comments = from c in albumModel.Comments
                            where c.CardId == parsedId
                            select c;

            List<CommentDTO> commentsDTOList = new List<CommentDTO>();
            foreach (var item in comments)
            {
                string commentAuthor = albumModel.Users.FirstOrDefault(x => x.Id == item.UserId).Username;
                CommentDTO currentComment = new CommentDTO
                {
                    Author = commentAuthor,
                    Title = item.Title,
                    Content = item.Contents
                };
                commentsDTOList.Add(currentComment);
            }

            User user = albumModel.Users.FirstOrDefault(x => x.Id == card.UploaderId);
            string cardAuthor = user.Username;
            CardDTO cardDTO = new CardDTO
            {
                    Description = card.Description,
                    UploadDate = card.UploadDate.ToString(),
                    Comments = commentsDTOList,
                    UploaderName = cardAuthor
            };

            return cardDTO;
        }
    }
}
